import random

WIDTH = 800
HEIGHT = 800

BACK_COLOR = (0, 0, 0)
r = 128
g = 128
b = 128

rstep = 0.1
gstep = -0.2
bstep = 0.3

class MyCircle:
    def __init__(self, x, y, r):
        self.pos = [x, y]
        self.r = r 
        self.spd_x = 1
        self.spd_y= 1
    def SetSpeed(self, spd_x, spd_y):
        self.spd_x = spd_x
        self.spd_y = spd_y
        
    def Update(self):
        self.pos[0] += self.spd_x
        self.pos[1] += self.spd_y
        if (self.pos[0] + self.r > WIDTH) or (self.pos[0] - self.r < 0):
            self.spd_x = -(self.spd_x + self.spd_x * (random.random() - 0.5) * 0.1)
            # self.spd_x += self.spd_x * (random.random() - 0.5)
        elif (self.pos[1] + self.r > HEIGHT) or (self.pos[1] - self.r < 0):
            self.spd_y = -(self.spd_y + self.spd_y * (random.random() - 0.5) * 0.1)
            # self.spd_y += self.spd_y * (random.random()- 0.5)
        
        if self.pos[0] + self.r > WIDTH:
            self.pos[0] = WIDTH - self.r
        elif self.pos[0] - self.r < 0:
            self.pos[0] = self.r

        if self.pos[1] + self.r > HEIGHT:
            self.pos[1] = HEIGHT - self.r
        elif self.pos[1] - self.r < 0:
            self.pos[1] = self.r
        
    def Draw(self):
        screen.draw.filled_circle(self.pos, self.r, (r, g, b))

circle_list = []
for i in range(4):
    r = 1
    clr_index = random.randint(0, 6)
    circle = MyCircle(random.randint(50, 150), random.randint(50, 150), r)
    circle.SetSpeed(random.randint(1, 3), random.randint(1, 3))
    circle_list.append(circle)



def on_key_down(key):
    if key == keys.ESCAPE:
        quit()
        
def on_mouse_move(pos):
    print("mouse_down", pos)
        
def on_mouse_down(pos):
    print("mouse_down", pos)
    
def on_mouse_up(pos):
    print("mouse_up", pos)
    
def update():
    for c in circle_list:
        c.Update()
        
def draw():
    global r, g, b
    global rstep, gstep, bstep
    screen.clear()
    screen.fill(BACK_COLOR)
    count = len(circle_list)
    for i in range(count):
        print(r, g, b)
        if i == count - 1:
            screen.draw.line(circle_list[i].pos, circle_list[0].pos, (r, g, b))
        else:
            screen.draw.line(circle_list[i].pos, circle_list[i + 1].pos, (r, g, b))
        
        r += rstep
        g += gstep
        b += bstep

        if r < 0:
            r = 0
            rstep = -rstep 
        elif r > 255:
            r = 255
            rstep = -rstep

        if g < 0:
            g = 0
            gstep = -gstep
        elif g > 255:
            g = 255
            gstep = -gstep

        if b < 0:
            b = 0
            bstep = -bstep
        elif b > 255:
            b = 255
            bstep = -bstep


        



